package com.xiu.travel.util;


import com.xiu.travel.config.MinioProperties;
import io.minio.*;
import io.minio.errors.*;
import io.minio.http.Method;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Component
@Slf4j
public class MinioClientUtil
{

    @Autowired
    private MinioProperties properties;

    private MinioClient client;

    //初始化minio client
    private void init() throws NoSuchAlgorithmException, InvalidKeyException, IOException,
            InvalidResponseException, InsufficientDataException, ServerException, ErrorResponseException,
            XmlParserException, InternalException
    {
        if (null != client)
        {
            return;
        }

        log.info("properties:{}", properties);
        client = MinioClient.builder()
                .endpoint(properties.getEndpoint())
                .credentials(properties.getAccessKey(), properties.getSecretKey())
                .build();

        makeBucket(properties.getBucketName());
    }

    //创建自定义的存储桶
    //这里只做了简单的桶存在判断，这里还可以添加设置桶的策略等
    public void makeBucket(String bucket) throws IOException, InvalidKeyException, InvalidResponseException,
            InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException,
            ErrorResponseException
    {
        boolean bucketExist = client.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
        if (!bucketExist)
        {
            client.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
        }
    }

    /**
     * @return void
     * @Description
     * @Date 11:58 2022/8/19
     * @Param [bucket=同名称, fileName=文件路径即名称, ins=上传的输入流, fileSize=上传的文件大小]
     **/
    public void uploadFile(String bucket, String fileName, InputStream ins, long fileSize)
            throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException,
            NoSuchAlgorithmException, ServerException, InternalException, XmlParserException,
            ErrorResponseException
    {

        init();
        makeBucket(bucket);

        PutObjectArgs.Builder putObjectArgsBuilder = PutObjectArgs.builder()
                .bucket(bucket)
                .object(fileName)
                .stream(ins, fileSize, 5 * 1024 * 1024);

        client.putObject(putObjectArgsBuilder.build());
    }

    /**
     * @return java.io.InputStream
     * @Description
     * @Date 12:09 2022/8/19
     * @Param [bucket=存储桶名称, fileName=文件路径即名称]
     **/
    public InputStream download(String bucket, String fileName)
            throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException,
            NoSuchAlgorithmException, ServerException, InternalException, XmlParserException,
            ErrorResponseException
    {

        init();

        GetObjectArgs.Builder getObjectArgsBuilder = GetObjectArgs.builder()
                .bucket(bucket)
                .object(fileName);

        return client.getObject(getObjectArgsBuilder.build());
    }


    /**
     * @return java.lang.String
     * @Description
     * @Date 12:14 2022/8/19
     * @Param [bucket=存储桶名称, fileName=文件路径即名称]
     **/
    private String getMinioURL(String bucket, String fileName) throws IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, ServerException, XmlParserException, InternalException, InsufficientDataException, ErrorResponseException
    {
        if (null == client)
        {
            init();
        }

        GetPresignedObjectUrlArgs build = GetPresignedObjectUrlArgs.builder()
                .method(Method.GET)
                .bucket(bucket)
                .object(fileName)
                //生成的预签名url可访问的有效时间，最大期限7天
                .expiry(60 * 60 * 24)
                .build();
        return client.getPresignedObjectUrl(build);
    }


}